package com.bluestone.action;

import java.util.HashMap;
import java.util.Iterator;

import com.bluestone.BaseAction;
import com.bluestone.assertor.IAssertor;
import com.bluestone.context.ConfigContext;
import com.bluestone.context.IContext;
import com.bluestone.run.RunManager;
import com.bluestone.scripts.ActionScript;
import com.bluestone.util.Util;

public class AssertAction extends BaseAction {

	public AssertAction(ActionScript action) {
		super(action);
		// TODO Auto-generated constructor stub
	}
	
	public boolean execute(IContext context) {
		HashMap<String,IAssertor> map = ConfigContext.getInstance().getAssertors();
		IAssertor assertor = null;
		String target = action.getPara("target");
		if(target !=null && target.length()>0){
			assertor = (IAssertor) map.get(target);
		}
		if(assertor==null){
			Iterator iterator = map.values().iterator();
			while(iterator.hasNext()){
				IAssertor temp =((IAssertor)iterator.next());
				if(temp.isDefault()){
					assertor = temp;
					break;
				}
			}
		}
		if(assertor!=null){
			String value = action.getPara("value");
			if(value!=null && value.startsWith("@")){
				value = Util.getString(value).trim();
			}
			//Util.getLogger().info("AssertAction:"+action.getType()+"-->"+value);
			return assertor.assertExecute(action.getType(), value);
		}else{
			return false;
		}
	}	

}
